//
//  UserPage.swift
//  FeastBooking
//
//  Created by Imon on 2017/7/7.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

class PersonMenu: UIView {
    
    let titleLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = UIColor.gray
        return label
    }()
    
    let pointerLabel: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.image = UIImage(named: "pointer")
        return imageView
    }()
    
    let saperator: UIView = {
        let view = UIView()
        view.backgroundColor = saperatorColor
        return view
    }()
    
    let redPoint: UIView = {
        let view = UIView()
        view.backgroundColor = redPointColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubview(titleLabel)
        self.addSubview(pointerLabel)
        self.addSubview(saperator)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let pointerWidth:CGFloat = 13
        let textMaxSize = CGSize(width: bounds.width, height: 40)
        let titleWidth = TextSize.textSize(text: titleLabel.text!, font: AppFont(size: 14), maxSize: textMaxSize).width
        titleLabel.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(5, 15, 5, bounds.width - 15 - titleWidth))
        pointerLabel.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(5, bounds.width - 10 - pointerWidth, 5, 10))
        saperator.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(bounds.height - 0.5, 10, 0, 10))
        let redPointSize: CGFloat = 8
        redPoint.frame = CGRect(x: titleLabel.frame.origin.x + titleLabel.frame.width - redPointSize / 2, y: titleLabel.frame.origin.y + 8, width: redPointSize, height: redPointSize)
        redPoint.layer.cornerRadius = redPointSize / 2
        redPoint.layer.masksToBounds = true
    }
    
    func showRedPoint() {
        self.addSubview(redPoint)
    }
    
    func hideRedPoint() {
        redPoint.removeFromSuperview()
    }
}

class CashBackMenu: UIView {
    
    let lifeLongTitleLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = darkTextColor
        label.text = LanguageHelper.getString(key: "TOTAL_CASHBACK")
        return label
    }()
    
    let lifeLongBalanceLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = UIColor.gray
        label.text = "$0.0"
        return label
    }()
    
    let pendingTitleLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = darkTextColor
        label.text = LanguageHelper.getString(key: "PENDING_CASHBACK")
        return label
    }()
    
    let pendingBalanceLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = UIColor.gray
        label.text = "$0.0"
        return label
    }()
    
    let finishedTitleLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = darkTextColor
        label.text = LanguageHelper.getString(key: "RECEIVED_CASHBACK")
        return label
    }()
    
    let finishedBalanceLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = UIColor.gray
        label.text = "$0.0"
        return label
    }()
    
    let saperator1: UIView = {
        let view = UIView()
        view.backgroundColor = saperatorColor
        return view
    }()
    
    let saperator2: UIView = {
        let view = UIView()
        view.backgroundColor = saperatorColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubview(lifeLongTitleLabel)
        self.addSubview(lifeLongBalanceLabel)
        self.addSubview(finishedTitleLabel)
        self.addSubview(finishedBalanceLabel)
        self.addSubview(pendingTitleLabel)
        self.addSubview(pendingBalanceLabel)
        self.addSubview(saperator1)
        self.addSubview(saperator2)
    }
    
    func initValue(userCashback: UserCashBack) {
        if let total = userCashback.totalCashback {
            lifeLongBalanceLabel.text = "$\(total)"
        } else {
            lifeLongBalanceLabel.text = "$0.0"
        }
        if let complete = userCashback.completedCashback {
            finishedBalanceLabel.text = "$\(complete)"
        } else {
            finishedBalanceLabel.text = "$0.0"
        }
        if let pending = userCashback.pendingCashback {
            pendingBalanceLabel.text = "$\(pending)"
        } else {
            pendingBalanceLabel.text = "$0.0"
        }
        layoutSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let textMaxSize = CGSize(width: bounds.width, height: 40)
        let lifeLongTitleSize = TextSize.textSize(text: lifeLongTitleLabel.text!, font: lifeLongTitleLabel.font, maxSize: textMaxSize)
        let lifeLongBalanceSize = TextSize.textSize(text: lifeLongBalanceLabel.text!, font: lifeLongBalanceLabel.font, maxSize: textMaxSize)
        let finishedTitleSize = TextSize.textSize(text: finishedTitleLabel.text!, font: finishedTitleLabel.font, maxSize: textMaxSize)
        let finishedBalanceSize = TextSize.textSize(text: finishedBalanceLabel.text!, font: finishedBalanceLabel.font, maxSize: textMaxSize)
        let pendingTitleSize = TextSize.textSize(text: pendingTitleLabel.text!, font: pendingTitleLabel.font, maxSize: textMaxSize)
        let pendingBalanceSize = TextSize.textSize(text: pendingBalanceLabel.text!, font: pendingBalanceLabel.font, maxSize: textMaxSize)
        
        let oneThirdWidth = self.bounds.width / 3
        let gapToTop: CGFloat = 10
        saperator1.frame = CGRect(x: oneThirdWidth, y: gapToTop, width: 0.5, height: self.bounds.height - 2 * gapToTop)
        saperator2.frame = CGRect(x: oneThirdWidth * 2, y: gapToTop, width: 0.5, height: self.bounds.height - 2 * gapToTop)
        
        lifeLongTitleLabel.frame = CGRect(x: (oneThirdWidth - lifeLongTitleSize.width) / 2, y: gapToTop, width: lifeLongTitleSize.width, height: lifeLongTitleSize.height)
        lifeLongBalanceLabel.frame = CGRect(x: (oneThirdWidth - lifeLongBalanceSize.width) / 2, y: self.bounds.height - gapToTop - lifeLongBalanceSize.height, width: lifeLongBalanceSize.width, height: lifeLongBalanceSize.height)
        
        finishedTitleLabel.frame = CGRect(x: (oneThirdWidth - finishedTitleSize.width) / 2 + oneThirdWidth, y: gapToTop, width: finishedTitleSize.width, height: finishedTitleSize.height)
        finishedBalanceLabel.frame = CGRect(x: (oneThirdWidth - finishedBalanceSize.width) / 2 + oneThirdWidth, y: self.bounds.height - gapToTop - finishedBalanceSize.height, width: finishedBalanceSize.width, height: finishedBalanceSize.height)
        
        pendingTitleLabel.frame = CGRect(x: (oneThirdWidth - pendingTitleSize.width) / 2 + oneThirdWidth * 2, y: gapToTop, width: pendingTitleSize.width, height: pendingTitleSize.height)
        pendingBalanceLabel.frame = CGRect(x: (oneThirdWidth - lifeLongBalanceSize.width) / 2 + oneThirdWidth * 2, y: self.bounds.height - gapToTop - pendingBalanceSize.height, width: pendingBalanceSize.width, height: pendingBalanceSize.height)
    }
}
